<template>
	<view class="page">
		<view style="padding: 0 30rpx;">
			<u--form :borderBottom="true" :model="form" ref="uForm" labelPosition="top">
				<u-form-item :borderBottom="true" prop="mobile" label="手机号" labelWidth="180">
					<u-input border="none" v-model="form.mobile" placeholder="请输入手机号" />
				</u-form-item>
				<u-form-item :borderBottom="true" prop="sms_code" label="验证码" labelWidth="180">
					<u-input border="none" v-model="form.sms_code" placeholder="请输入验证码" />
				</u-form-item>
			</u--form>
			<view style="margin-top: 90rpx;">
				<u-button @click="submit" shape="circle" color="linear-gradient(to left, #e44e15, #f4aa56)" v-if="!visible" text="验证"></u-button>
				<u-button @click="submit2" shape="circle" color="linear-gradient(to left, #e44e15, #f4aa56)" v-if="visible" text="验证"></u-button>
			</view>
		</view>
		<u-toast ref="uToast"></u-toast>
	</view>
</template>

<script>
	import imagesUrl from '@/common/images.js'
	import {rightCard, cashierCon, planCashierCard} from '@/common/creditcardmachine.api.js'
	export default {
		data() {
			return {
				form: {
					mobile:'',
					verification_code: '',
					sms_code: '',

				},
				leftIconStyle: {
					color: '#999'
				},
				visible: false,
				user: {},
				seconds: 60,
				codeTips: '获取验证码',
				defultJson: {
					type: 0, //0制定收银计划要求验证码   1 绑卡以及之后曹错 3确认智还计划 确定绑卡验证码
				},
			}
		},
		onLoad(option) {
			this.defultJson = JSON.parse(option.defultJson)
			this.form.mobile = this.defultJson.mobile;
		},
		methods: {
			codeChange(text) {
				this.codeTips = text;
			},
			submit() {
				// uni.showLoading({
				// 	title: '正在绑定'
				// })
				uni.hideLoading()
				let __this = this;

				// 0-end.已制定收银计划 需要验证码进行 确定收银计划 
				if (this.defultJson.type == 0) {
					this.statusSms()
				}
				// 1-end  确认绑卡-制定收银计划- 确认收银计划
				if (this.defultJson.type == 1) {
					this.rightCard()
				}
				// 2-end 绑卡操作携带plan—id 直接进行确认智还计划
				if (this.defultJson.type == 2) {
					this.zhihuan()
				}

			},
			submit2() {
				this.statusSms()
			},
			// 确认绑卡
			rightCard() {
				console.log(111)
				let that = this;
				let params = {
					channel_bankcard_id: __this.defultJson.channel_bankcard_id,
					sms_code: __this.form.sms_code
				}
				rightCard(params).then(res => {
					console.log(1111,res)
					let params2 = this.defultJson;
					// 制定收银计划
					cashierCon(params2).then(res2 => {
						console.log(cashierCon,res2)
						if (res2.type == 'sms') {
							let defultJson2 = this.defultJson;
							defultJson2.type = 0;
							defultJson2.plan_id = res2.plan_id
							that.visible = true;
							uni.showToast({
								title: '已发送验证码～',
								icon: 'none',
							})
							this.statusSms()
						}
						if (res2 == 'BIND_CARD_AUTH') {
							this.statusSms()
						}

					})
				})
			},
			// 已制定收银计划 需要验证码进行 确定收银计划 
			statusSms() {
				let dataJson = this.defultJson
				let params3 = {
					plan_id: dataJson.plan_id,
					sms_code: this.form.sms_code,
				}
				let that = this
				
				uni.showLoading({
					title:'验证中...'
				})
				
				planCashierCard(params3).then(res => {
					that.visible = false;
					uni.hideLoading()
					that.$refs.uToast.show({
						type: 'success',
						message: "已提交,请稍后查看到账信息",
						icon: 'https://cdn.uviewui.com/uview/demo/toast/success.png',
						complete(){
							that.$u.route({
								url: 'pages/creditcard/cashier/list'
							})
						}
					})
				})
			},
			// 确认智还计划
			zhihuan() {
				console.log(333)
				let __this = this;
				let params = {
					channel_bankcard_id: __this.defultJson.channel_bankcard_id,
					sms_code: __this.form.sms_code,
					plan_id: __this.defultJson.plan_id
				}
				console.log(__this.form)
				rightCard(params).then(res => {
					__this.visible = false;
					__this.$refs.uToast.show({
						type: 'success',
						message: "制定成功",
						icon: 'https://cdn.uviewui.com/uview/demo/toast/success.png',
						complete(){
							__this.$u.route({
								url: 'pages/creditcard/autoPayMoney/list'
							})
						}
					})
				}).catch(err=>{
					console.log(err)
				})
			},
			showToast(params) {
				this.$refs.uToast.show({
					...params,
					complete() {
						params.url && uni.navigateTo({
							url: params.url
						})
					}
				})
			}
		}
	}
</script>


<style lang="scss">
	@import "../../static/iconfont.css";

	.page {
		background-color: #fff;
		min-height: 100Vh;
		width: 100vw;
	}

	.footer {
		margin-top: 32upx;
		font-size: 24upx;
		color: #666666;
		text-align: center;
		display: flex;
		justify-content: center;
	}

	.submit {
		margin-top: 30rpx;
	}

	// .leftIconStyle {
	// 	width: 30rpx;
	// 	height: 30rpx;
	// 	align-items: center;
	// }
</style>
